package org.phalanx.client.mapeditor;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.phalanx.map.MapCoordinates;
import org.phalanx.map.MapTile;
import org.phalanx.map.MapTileSet;
import org.phalanx.map.PhalanxMap;
import org.phalanx.zip.ZipFileArchiveReader;

public class PhalanxTileInformationPanel extends JPanel {
	private JLabel terrainTypeLabel;
	private JLabel tileImageFrame;
	private JLabel tileName;
	private MapCoordinates location;
	private JLabel locationLabel;
	private boolean isDefault;
	private PhalanxMapEditorPanel editor;

	public PhalanxTileInformationPanel(PhalanxMapEditorPanel editor) {
		this.editor = editor;
		initializePanel();
	}

	private void initializePanel() {
		this.setBackground(Color.black);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(setUpImageArea());
		this.add(setUpTileNameGrid());
		this.add(setUpTileTypeGrid());
		this.add(setUpTileLocationGrid());
	}

	private JPanel setUpImageArea() {
		JPanel imagePanel = getDefaultSubPanel();

		tileImageFrame = new JLabel();

		imagePanel.add(getFormattedJLabels("Image:"));
		imagePanel.add(tileImageFrame);

		return imagePanel;
	}

	private JPanel setUpTileNameGrid() {
		JPanel tileNamePanel = getDefaultSubPanel();

		tileName = new JLabel();
		tileName.setForeground(Color.red);
		tileNamePanel.add(getFormattedJLabels("Tile Name: "));
		tileNamePanel.add(tileName);

		return tileNamePanel;
	}

	private JPanel setUpTileTypeGrid() {
		JPanel tileTypePanel = getDefaultSubPanel();

		terrainTypeLabel = new JLabel();
		terrainTypeLabel.setForeground(Color.red);
		tileTypePanel.add(getFormattedJLabels("Terrain:"));
		tileTypePanel.add(terrainTypeLabel);

		return tileTypePanel;
	}

	private JPanel setUpTileLocationGrid() {
		JPanel tileLocationPanel = getDefaultSubPanel();

		this.locationLabel = new JLabel();
		locationLabel.setForeground(Color.red);
		tileLocationPanel.add(getFormattedJLabels("Location: "));
		tileLocationPanel.add(locationLabel);

		return tileLocationPanel;
	}

	public void invokeData() {
		PhalanxMapEditorView pe = editor.getDisplayContainer()
				.getMapViewObject();
		PhalanxMap m = pe.getMap();
		if (m != null) {
			MapTileSet tileSet = m.getMapTileSet();
			if (tileSet != null) {
				if (pe.getSelectionArea() != null) {
					location = pe.getSelectionArea().getSelectionMatrixStart();
					MapTile thisTile = m.getMapTileAt(location);
					locationLabel.setText("x: " + location.getX() + " y: "
							+ location.getY());
					tileName.setText(m.getMapTileSet()
							.getTerrainRefNameFromTile(thisTile));
					tileImageFrame.setIcon(new ImageIcon(getTileImage(m,
							thisTile)));
					terrainTypeLabel.setText(thisTile.getTerrain()
							.getTerrainType());
				}
			}
		}
	}

	private BufferedImage getTileImage(PhalanxMap m, MapTile t) {
		BufferedImage b = null;
		try {
			ZipFileArchiveReader z = new ZipFileArchiveReader(m.getMapTileSet()
					.getTileSetArchiveFile());

			if (z.isFileExist()) {
				byte[] imageBytes = z.getArchivedFile(t.getImageReference());
				b = ImageIO.read(new ByteArrayInputStream(imageBytes));
			}
		} catch (IOException ie) {
		}

		return b;
	}

	private JPanel getDefaultSubPanel() {
		JPanel tempPanel = new JPanel();
		tempPanel.setBackground(Color.black);
		tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.Y_AXIS));
		return tempPanel;
	}

	private JLabel getFormattedJLabels(String text) {
		JLabel tempLabel = new JLabel(text);
		tempLabel.setForeground(Color.white);
		return tempLabel;
	}

}
